// component/rightSetting/index.js
wx.cloud.init({
  traceUser: true
})
const db = wx.cloud.database()
const rightinfo = db.collection('rightinfo')
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    loading: false,
    birthdayState: true,
    addressState: true,
    emotionState: true,
    schoolState: true,
    companyState: true,
    chatwayState: true
  },

  created: function(){
    this.getStateData()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getStateData(){
      rightinfo.where({
        openid: app.user._openid
      }).field({
        birthdayState: true,
        addressState: true,
        emotionState: true,
        schoolState: true,
        companyState: true
      }).get().then(res => {
        let data = res.data[0]
        this.setData({
          birthdayState: data.birthdayState,
          addressState: data.addressState,
          emotionState: data.emotionState,
          schoolState: data.schoolState,
          companyState: data.companyState,
        })
      })
    },
    comfireModified(){
      this.setData({
        loading: true
      })
      wx.cloud.callFunction({
        name: 'modifyRightState',
        data: {
          birthdayState: this.data.birthdayState,
          addressState: this.data.addressState,
          emotionState: this.data.emotionState,
          schoolState: this.data.schoolState,
          companyState: this.data.companyState,
        }
      }).then(res => {
        this.setData({
          loading: false
        })
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
      })
    },
    birthdaySwitch(e){
      this.setData({
        birthdayState: e.detail.value
      })
    },
    addressSwitch(e) {
      this.setData({
        addressState: e.detail.value
      })
    },
    emotionSwitch(e) {
      this.setData({
        emotionState: e.detail.value
      })
    },
    schoolSwitch(e) {
      this.setData({
        schoolState: e.detail.value
      })
    },
    companySwitch(e) {
      this.setData({
        companyState: e.detail.value
      })
    },
  }
})
